// pages/index/index.js
import { foodOutMsg } from '../../apis/index'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        msg:'Hello World',
        address:'成都',
        redRight:10,
        menuMTop:105,
        foodleftList:[],
        foodrightList:[],
        rightpage:2,
        leftpage:1
    },
    //关闭通知时触发
    colseMsg(){
        this.setData({menuMTop:105})
    },
    //扫一扫自定义函数
    scan:function(){
        wx.scanCode({
            onlyFromCamera: true,
            success:function(res){
                console.log(res);
            }
        })
    },
    //自定义函数
    getLocation:function(){
        console.log(11)
        var that = this;
        wx.chooseLocation({
            success:function(res){
                console.log("成功",res);
                that.setData({
                    address:res.name
                });
            }
        })
    },
    //打开红包的函数
    redBao:function(){
        if(this.data.redRight == 10){
            wx.showToast({
                title: '打开新页面'
            })
        }else{
            this.setData({
                redRight:10
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    getFoods(){
        let leftpage = this.data.leftpage;
        let rightpage = this.data.rightpage;
        foodOutMsg(leftpage,3).then(res =>{
            let leftList = this.data.foodleftList;
            leftList = leftList.concat(res.data)
            this.setData({
                foodleftList:leftList
            })
        })
        foodOutMsg(rightpage,3).then(res =>{
            let rightList = this.data.foodrightList;
            rightList = rightList.concat(res.data)
            this.setData({
                foodrightList:rightList
            })
        })
    },
    onLoad: function (options) {
      this.getFoods();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
       
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({
            leftpage:this.data.leftpage + 2 ,
            rightpage:this.data.rightpage + 2
        })
        this.getFoods();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //监听屏幕滚动 判断上下滚动
    onPageScroll: function (ev) {
        if(ev.scrollTop > 200){
            this.setData({
                redRight:-100
            });
        }else{
            this.setData({
                redRight:10
            });
        }
    }
})